package com.skcc.cop.mrtaxi.biz.destination;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;

import com.skcc.cop.mrtaxi.R;
import com.skp.Tmap.TMapData;
import com.skp.Tmap.TMapPOIItem;

public class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable{

	private static final String TAG = PlacesAutoCompleteAdapter.class.getSimpleName();
	private ArrayList<String> resultList;
	//	private ArrayList<String> resultPointList;
	private TMapData mTMapData;
	private ArrayList<TMapPOIItem> tMapList = new ArrayList<TMapPOIItem>();
	protected ProgressDialog progressDialog;
	Context context;

	public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
		// TODO Auto-generated constructor stub
		this.context = context;
		mTMapData = new TMapData();
	}


	@Override
	public Filter getFilter() {
		// TODO Auto-generated method stub
		Filter filter = new Filter(){

			@Override
			protected FilterResults performFiltering(CharSequence constraint) {
				// TODO Auto-generated method stub
				Log.i(TAG, "performFiltering()");
				FilterResults filterResults = new FilterResults();
				if(constraint != null){
					resultList = findAddressPoi(constraint.toString());

					//Assign the data to the FilterResults
					filterResults.values = resultList;

					//System.out.println("ku33333333 : " + filterResults.values);

					filterResults.count = resultList.size();
				}
				return filterResults;
			}

			@Override
			protected void publishResults(CharSequence constraint, FilterResults results) {
				
				Log.i(TAG, "publishResults()");
				// TODO Auto-generated method stub
				if(results != null && results.count > 0){
					notifyDataSetChanged();
				}else{
					notifyDataSetInvalidated();
				}
			}};
			return filter;

	}


	/**
	 * 주소검색 POI를 요청한다. 
	 */
	@SuppressWarnings("finally")
	public ArrayList<String> findAddressPoi(String strData)
	{
		
		Log.i(TAG, "findAddressPoi()");
		ArrayList<String> returnList = new ArrayList<String>();
		//		ArrayList<String> returnPointList = new ArrayList<String>();

		try {
			//showProgressDialog(context);
			tMapList = mTMapData.findAddressPOI(strData);
			Log.i(TAG, "tMapList.size() : " + tMapList.size());
			
			Thread.sleep(1000);
			//dismissProgressDialog();
			for(int i = 0; i < tMapList.size(); i++)
			{
				TMapPOIItem  item = tMapList.get(i);
				returnList.add(item.getPOIName());		
//        		LogManager.printLog(item.getPOIName() + " | " +
//        							item.getPOIID() + " | " + 
//        							item.getPOIAddress() + " | " + 
//        							item.getPOIContent() + " | " + 
//        							item.frontLat + " | " + 
//        							item.frontLon + " | " + 
//        							item.detailAddrName + " | " + 
//        							item.detailBizName);
			}

		}catch(SAXException e){
			e.printStackTrace();
		}catch(ParserConfigurationException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			if(returnList.size() > 0){
				Log.i(TAG, "findAddressPoi : " + returnList);
				return returnList;
			}else{
				return new ArrayList<String>();
			}
		}
	}




	/**
	 * 토탈검색 POI를 요청한다. 
	 */
	@SuppressWarnings("finally")
	public ArrayList<String> findAllPoi(String strData)
	{
		Log.i(TAG, "findAddressPoi()");
		ArrayList<String> returnList = new ArrayList<String>();

		try {

			tMapList = mTMapData.findAllPOI(strData);
			Log.i(TAG, "tMapList.size() : " + tMapList.size());
			Thread.sleep(1000);
			
			for(int i = 0; i < tMapList.size(); i++)
			{
				TMapPOIItem  item = tMapList.get(i);
				returnList.add(item.getPOIName());
			}

		}catch(SAXException e){
			e.printStackTrace();
		}catch(ParserConfigurationException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			if(returnList.size() > 0){

				return returnList;


			}else{

				return new ArrayList<String>();
			}
		}
	}


	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return resultList.size();
	}


	@Override
	public String getItem(int position) {
		// TODO Auto-generated method stub
		Log.i(TAG, "getItem()");
		return resultList.get(position);
	}

	public ArrayList<TMapPOIItem> gettMapList() {
		return this.tMapList;
	}
	
	
	public void showProgressDialog(Context context)
    {
        if (progressDialog == null || !progressDialog.isShowing())
        {
            try
            {
                progressDialog = ProgressDialog.show(context, null, context.getString(R.string.loading), false, false);
            } catch (Exception e)
            {
                // ignore this exception. maybe
                // android.view.WindowManager$BadTokenException will occurs when
                // activity is finishing.
            }
        }
    }
	
	 /**
     * dismiss a progress dialog if it is showing.
     */
    public void dismissProgressDialog()
    {
        if (progressDialog != null && progressDialog.isShowing())
        {
            progressDialog.dismiss();
        }
    }
	

}
